import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root' })

export class TableRowMergeComponent {
    constructor() { }

    mergeRowSet(param, zdata, callback) {
        for (const name of param.split(',')) {
            // 起始位置
            let text = '';
            for (let i = 0; i < zdata.length; i++) {
                if (i === 0) {
                    text = zdata[i][name];
                    zdata[i][name + '_Start'] = true;
                } else {
                    if (text === zdata[i][name]) {
                        zdata[i][name + '_Start'] = false;
                    } else {
                        text = zdata[i][name];
                        zdata[i][name + '_Start'] = true;
                    }
                }
            }
            // 结束位置
            let last = '';
            const array = [];
            for (let i = zdata.length - 1; i >= 0; i--) {
                zdata[i][name + '_Index'] = i + 1;
                if (last !== zdata[i][name]) {
                    last = zdata[i][name];
                    zdata[i][name + '_End'] = true;
                    array.unshift(zdata[i][name + '_Index']);
                }
            }
            // 跨多少行
            let index = 0;
            for (const item of zdata) {
                if (item[name + '_Start']) {
                    switch (index) {
                        case 0:
                            item[name + '_Count'] = array[index];
                            break;
                        default:
                            item[name + '_Count'] = array[index] - array[index - 1];
                            break;
                    }
                    index += 1;
                }
            }
        }
        callback(zdata);
    }
}